﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using QuanLyNganHangDe.DAL;
using QuanLyNganHangDe.BLL;
using QuanLyNganHangDe.DAL.Models;

namespace QuanLyNganHangDe.GUI
{
    public partial class FormDoiMatKhau : FormBaseDialog
    {
        private NguoiSuDung m_NguoiSuDung;

        public FormDoiMatKhau(NguoiSuDung nguoiSuDung)
        {
            InitializeComponent();

            if (nguoiSuDung == null)
            {
                throw new Exception("Tham số nguoiSuDung == NULL!");
            }
            txtTenDangNhap.Text = nguoiSuDung.TenDangNhap;
            cbxVaiTro.Text = nguoiSuDung.VaiTro_Description;
            txtMatKhau.Focus();
            m_NguoiSuDung = nguoiSuDung;
        }

        private void OnDongYClick(object sender, EventArgs e)
        {
            string matKhauCu = txtMatKhau.Text;
            string matKhauMoi = txtMatKhauMoi.Text;
            string matKhauMoi2 = txtMatKhauMoi2.Text;

            if (matKhauCu != m_NguoiSuDung.MatKhau)
            {
                DialogUtils.ShowError("Mật khẩu cũ không đúng!");
                txtMatKhau.Focus();
                return;
            }

            if (matKhauMoi != matKhauMoi2)
            {
                DialogUtils.ShowError("Bạn phải gõ hai mật khẩu mới giống nhau!");
                txtMatKhauMoi.Focus();
                return;
            }

            try
            {
                BizNguoiSuDung.DoiMatKhau(m_NguoiSuDung, matKhauMoi);
                m_NguoiSuDung.MatKhau = matKhauMoi;
                this.DialogResult = System.Windows.Forms.DialogResult.OK;
                this.Close();
            }
            catch
            {
                DialogUtils.ShowError("Không thể đổi mật khẩu được!");
            }
        }

        private void FormDoiMatKhau_Load(object sender, EventArgs e)
        {

        }

        private void OnKeyDown(object sender, KeyEventArgs e)
        {
            if (e.KeyCode == Keys.Enter)
            {
                e.SuppressKeyPress = true;
                btnDongY.PerformClick();
            }
        }
    }
}
